package com.archerdu.commons.util;

import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

/**
 * servlet工具类
 * 
 * @projectName springDemo
 * @description servlet工具类
 * @author archer du
 * @createDate 2013年9月27日 下午5:21:36
 * @updateUser dxf
 * @updateDate 2013年9月27日 下午5:21:36
 * @updateRemark 新建
 * @version 0.0.1
 */
public class ServletUtil {

    /**
     * 获取所有ip
     * 
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取客户端真实ip
     * 
     * @param request
     * @return
     */
    public static String getClientIpAddr(HttpServletRequest request) {
        String ips = getIpAddr(request);
        return ips.split(",")[0];
    }

    /**
     * 获取请求参数，null值时返回""，方便判断
     * 
     * @param req
     * @param name
     *            参数名称
     * @return
     */
    public static String getParameter(HttpServletRequest req, String name) {
        String str = req.getParameter(name);
        if (null == str)
            str = "";
        return str;
    }

    /**
     * 将参数转化为json字符串格式
     * 
     * @param req
     * @param encryptParas
     *            需要加密的参数名称集合
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String parameter2Json(HttpServletRequest req, Set<String> encryptParas) {
        StringBuffer sb = new StringBuffer("[");

        Map<String, String[]> paras = req.getParameterMap();

        for (String name : paras.keySet()) {
            sb.append(name);
            sb.append(":");
            // 读取参数数组
            String[] values = paras.get(name);
            if (values.length > 1) {
                sb.append("[");
                for (String value : values) {
                    if (null != encryptParas && encryptParas.contains(name)) {
                        sb.append(SHA256.encrypt(value));
                    } else {
                        sb.append(value);
                    }
                    sb.append(",");
                }
                sb.deleteCharAt(sb.length() - 1);
                sb.append("]");
            } else if (values.length > 0) {
                if (null != encryptParas && encryptParas.contains(name)) {
                    sb.append(SHA256.encrypt(values[0]));
                } else {
                    sb.append(values[0]);
                }
            }
            sb.append(",");
        }
        // 删除最后一个逗号
        if (sb.length() > 1) {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("]");
        return sb.toString();
    }

    /**
     * 将参数转化为json字符串格式
     * 
     * @param req
     * @return
     */
    public static String parameter2Json(HttpServletRequest req) {
        return parameter2Json(req, null);
    }
}
